package com.jic.market.impl;

import com.google.common.collect.Lists;
import com.jic.market.entity.PromotionEntity;
import com.jic.market.entity.PromotionLimitRuleEntity;
import com.jic.market.entity.PromotionSingleRuleEntity;
import com.jic.market.mapper.PromotionEntityMapper;
import com.jic.market.mapper.PromotionLimitRuleEntityMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.mapper.PromotionSingleRuleEntityMapper;
import com.jic.market.request.PromotionLimitRuleRequest;
import com.jic.market.service.PromotionLimitRuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * description: PromotionLimitRuleServiceImpl
 * date: 2020/1/16 19:24
 * author: admin
 * version: 1.0
 */
@Service
public class PromotionLimitRuleServiceImpl implements PromotionLimitRuleService {
    private final  static List<Integer> canChangeStatus = Lists.newArrayList(1,2,3,4);
    @Autowired
    PromotionEntityMapper promotionEntityMapper;
    @Autowired
    PromotionSingleRuleEntityMapper promotionSingleRuleEntityMapper;
    @Autowired
    PromotionLimitRuleEntityMapper promotionLimitRuleEntityMapper;

    @Override
    public void updatePriceOrLimit(PromotionLimitRuleRequest ruleRequest) {
        //查询状态
//        PromotionEntity promotionEntity = promotionEntityMapper.queryById(ruleRequest.getPromotionId());
//
//        if(promotionEntity == null){
//            return;
//        }
//        if(promotionEntity == null || !canChangeStatus.contains(promotionEntity.getStatus())){
//            return;
//        }
//        if(ruleRequest.getContentType() != null){
//            PromotionSingleRuleEntity singleRuleEntity = new PromotionSingleRuleEntity();
//            singleRuleEntity.setPromotionId(ruleRequest.getPromotionId());
//            singleRuleEntity.setMpId(ruleRequest.getMpId());
//            singleRuleEntity.setContentValue(ruleRequest.getPromPrice());
//            promotionSingleRuleEntityMapper.updatePrice(singleRuleEntity);
//        }

        PromotionLimitRuleEntity limitRuleEntity = new PromotionLimitRuleEntity();
        BeanUtils.copyProperties(ruleRequest, limitRuleEntity);
        limitRuleEntity.setProductId(ruleRequest.getMpId());
        limitRuleEntity.setTotalLimitAmount(ruleRequest.getPromPrice());
        promotionLimitRuleEntityMapper.updateLimit(limitRuleEntity);
    }

    @Override
    public void delete(List<Long> ids) {
        ids.forEach(x->{
            promotionLimitRuleEntityMapper.delete(x);
        });
    }

    @Override
    public Integer addPromotionLimitRule(PromotionLimitRuleEntity promotionLimitRule) {
        return promotionLimitRuleEntityMapper.insert(promotionLimitRule);
    }

    @Override
    public PromotionLimitRuleEntity getEntityByPromotionIdAndMpId(Long promotionId, Long mpId) {
        return promotionLimitRuleEntityMapper.getEntityByPromotionIdAndMpId(promotionId, mpId);
    }

    @Override
    public Integer deleteEntityByPromotionIdAndMpId(Long promotionId, Long productId) {
        promotionLimitRuleEntityMapper.deleteEntityByPromotionIdAndMpId(promotionId, productId);
        return null;
    }

    @Override
    public Long insert(PromotionLimitRuleRequest ruleRequest) {
        PromotionLimitRuleEntity ruleEntity = new PromotionLimitRuleEntity();
        BeanUtils.copyProperties(ruleRequest, ruleEntity);
        ruleEntity.setLimitRef(ruleRequest.getMpId());
        ruleEntity.setLimitType(ruleRequest.getLimitType());
        if(ruleEntity.getId() == null){
            int insert = promotionLimitRuleEntityMapper.insert(ruleEntity);

        }else{
            promotionLimitRuleEntityMapper.update(ruleEntity);
        }
        if(ruleRequest.getContentValue() != null){
            //单一促销规则
            PromotionSingleRuleEntity limitEntitySingle = new PromotionSingleRuleEntity();
            limitEntitySingle.setPromotionId(ruleRequest.getPromotionId());
            limitEntitySingle.setProductId(ruleRequest.getMpId());
            limitEntitySingle.setMpId(ruleRequest.getMpId());
            limitEntitySingle.setContentValue(ruleRequest.getContentValue());
            limitEntitySingle.setCreator(ruleRequest.getCreator());
            limitEntitySingle.setCreatorName(ruleRequest.getCreatorName());
            limitEntitySingle.setUpdator(ruleRequest.getCreator());
            limitEntitySingle.setCreateTime(new Date());
            limitEntitySingle.setTypeOfProduct(ruleRequest.getLimitType() == 5 ? 1 : 2);
            //判断是否存在
            PromotionSingleRuleEntity param = new PromotionSingleRuleEntity();
            param.setMpId(ruleRequest.getMpId());
            param.setTypeOfProduct(limitEntitySingle.getTypeOfProduct());
            param.setPromotionId(ruleRequest.getPromotionId());
            param.setDeleteFlag(0);
            List<PromotionSingleRuleEntity> all = promotionSingleRuleEntityMapper.findAll(param);
            if(CollectionUtils.isNotEmpty(all)){
                all.forEach(x->{
                    x.setContentValue(ruleRequest.getContentValue());
                    promotionSingleRuleEntityMapper.update(x);
                });
            }else{
                promotionSingleRuleEntityMapper.insert(limitEntitySingle);
            }
        }
        return ruleEntity.getId();
    }
}
